home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / TouchMe 1.2□ / touchMe 1.2 Folder / touchMe source codes / CW11 PP source / source / Common Lib / CDateEditField.cp < prev    next >
Encoding:
Text File  |  1997-04-25  |  9.7 KB  |  428 lines  |  [TEXT/CWIE]

  1. // ==================================================
  2. //    CDateEditField.cp
  3. //    Copyright (C) 1996-1997 Mizutori Tetsuya
  4. //    July 4, 1996; February 3, 1997.
  5. // ==================================================
  6. //    All documents are pretty-printed in 10-point Geneva font.
  7.  
  8. #include <LDialogBox.h>
  9. #include <UDrawingState.h>
  10.  
  11. #include "touchMeConstants.h"
  12. #include "CDateEditField.h"
  13. #include "UDateTime.h"
  14. #include "UDragDropSuit.h"
  15.  
  16. const ResIDT    rcrsr_DraggingHand        = 1001;
  17.  
  18.  
  19. // --------------------------------------------------
  20. //        • CreateDateEditFieldStream [static]
  21. // --------------------------------------------------
  22.  
  23. CDateEditField *
  24. CDateEditField::CreateDateEditFieldStream(
  25.     LStream *    inStream )
  26. {
  27.     return new CDateEditField( inStream );
  28. }
  29.  
  30.  
  31. // --------------------------------------------------
  32. //        • CDateEditField
  33. // --------------------------------------------------
  34.  
  35. CDateEditField::CDateEditField()
  36. {
  37.     mHasChanged = false;
  38. }
  39.  
  40.  
  41. // --------------------------------------------------
  42. //        • CDateEditField(const CDateEditField&)
  43. // --------------------------------------------------
  44.  
  45. CDateEditField::CDateEditField(
  46.     const CDateEditField &        inOriginal )
  47.         : LEditField( inOriginal ),  LListener( inOriginal ), LBroadcaster( inOriginal )
  48. {
  49.     mHasChanged = false;
  50. }
  51.  
  52.  
  53. // --------------------------------------------------
  54. //        • CDateEditField(SPaneInfo&, ... , inHasWordWrap, ...)
  55. // --------------------------------------------------
  56.  
  57. CDateEditField::CDateEditField(
  58.     const SPaneInfo &    inPaneInfo,
  59.     Str255            inString,
  60.     ResIDT            inTextTraitsID,
  61.     Int16            inMaxChars,
  62.     Boolean            inHasBox,
  63.     Boolean            inHasWordWrap,
  64.     KeyFilterFunc        inKeyFilter,
  65.     LCommander *        inSuper)
  66.         : LEditField( inPaneInfo, inString, inTextTraitsID, inMaxChars,
  67.                 inHasBox, inHasWordWrap, inKeyFilter, inSuper)
  68. {
  69.     mHasChanged = false;
  70. }
  71.  
  72.  
  73. // --------------------------------------------------
  74. //        • CDateEditField(SPaneInfo&, ... , inAttributes, ...)
  75. // --------------------------------------------------
  76.  
  77. CDateEditField::CDateEditField(
  78.     const SPaneInfo &    inPaneInfo,
  79.     Str255            inString,
  80.     ResIDT            inTextTraitsID,
  81.     Int16            inMaxChars,
  82.     Uint8            inAttributes,
  83.     KeyFilterFunc        inKeyFilter,
  84.     LCommander *        inSuper)
  85.         : LEditField( inPaneInfo, inString, inTextTraitsID, inMaxChars,
  86.                 inAttributes, inKeyFilter, inSuper)
  87. {
  88.     mHasChanged = false;
  89. }
  90.  
  91.  
  92. // --------------------------------------------------
  93. //        • CDateEditField(LStream*)
  94. // --------------------------------------------------
  95.  
  96. CDateEditField::CDateEditField(
  97.     LStream *        inStream )
  98.         : LEditField( inStream )
  99. {
  100.     mHasChanged = false;
  101. }
  102.  
  103.  
  104. // --------------------------------------------------
  105. //        • ~CDateEditField
  106. // --------------------------------------------------
  107.  
  108. CDateEditField::~CDateEditField()
  109. {
  110. }
  111.  
  112.  
  113. // --------------------------------------------------
  114. //        • ListenToMessage
  115. // --------------------------------------------------
  116. void
  117. CDateEditField::ListenToMessage(
  118.     MessageT    inMessage,
  119.     void *    ioParam )
  120. {
  121. #pragma unused ( inMessage, ioParam )
  122.  
  123.     //PaneIDT    thisPaneID = GetPaneID();
  124.     //Int32    thisUserCon = GetUserCon();
  125.  
  126.     // Do not refresh the EditField if click is still in this EditField.
  127.     //if ( inMessage == thisUserCon ) return;
  128.  
  129.     //RefreshEditField();
  130. }
  131.  
  132.  
  133. // --------------------------------------------------
  134. //        • UserChangedText
  135. // --------------------------------------------------
  136.  
  137. void
  138. CDateEditField::UserChangedText( void )
  139. {
  140.     LEditField::UserChangedText();
  141.  
  142.     mHasChanged = true;
  143. }
  144.  
  145.  
  146. // --------------------------------------------------
  147. //        • ClickSelf
  148. // --------------------------------------------------
  149.  
  150. void
  151. CDateEditField::ClickSelf(
  152.     const SMouseDownEvent &    inMouseDown )
  153. {
  154.     if ( DoDragText( inMouseDown.macEvent, inMouseDown.whereLocal ) ) return;
  155.  
  156.     LEditField::ClickSelf( inMouseDown );
  157.  
  158. //    Int32    thisUserCon = GetUserCon();
  159. //    BroadcastMessage( (MessageT) thisUserCon, nil );
  160. }
  161.  
  162.  
  163. // --------------------------------------------------
  164. //        • BeTarget
  165. // --------------------------------------------------
  166.  
  167. void
  168. CDateEditField::BeTarget( void )
  169. {
  170.     LEditField::BeTarget();
  171.  
  172. //    Int32    thisUserCon = GetUserCon();
  173. //    BroadcastMessage( (MessageT) thisUserCon, nil );
  174. }
  175.  
  176.  
  177. // --------------------------------------------------
  178. //        • DontBeTarget
  179. // --------------------------------------------------
  180.  
  181. void
  182. CDateEditField::DontBeTarget( void )
  183. {
  184.     LEditField::DontBeTarget();
  185.  
  186. //    Int32    thisUserCon = GetUserCon();
  187. //    BroadcastMessage( (MessageT) thisUserCon, nil );
  188.  
  189.     long        theSelStart, theSelEnd;
  190.     GetSelect( theSelStart, theSelEnd );
  191.     RefreshEditField();
  192.     SetSelect( theSelStart, theSelEnd );
  193. }
  194.  
  195.  
  196. // --------------------------------------------------
  197. //        • DoDragText
  198. // --------------------------------------------------
  199.  
  200. Boolean
  201. CDateEditField::DoDragText(
  202.     const EventRecord &    inMacEvent,
  203.     const Point            inMousePosition )
  204. {
  205.  
  206.     // Change cursor shape to my 'grabbing hand'.
  207. //    ::SetCursor( &(qd.arrow));
  208.     CCrsrHandle    theCursH = ::GetCCursor( rcrsr_DraggingHand );
  209.     if ( theCursH != NULL ) {
  210.         ::SetCCursor( theCursH );
  211.         ::DisposeCCursor( theCursH );
  212.     }
  213.  
  214.     OSErr    err;
  215.     Boolean    result = false;
  216.  
  217.     TEHandle    teH = GetMacTEH();
  218.  
  219.     RgnHandle    hiliteRgn = ::NewRgn();
  220.     err = ::TEGetHiliteRgn( hiliteRgn, teH );
  221.     if ( ::PtInRgn( inMousePosition, hiliteRgn ) ) {
  222.         Handle        hText = (**teH).hText;
  223.         unsigned char *    textP = (unsigned char *) *hText + (**teH).selStart;
  224.         long            textLen = (**teH).selEnd - (**teH).selStart;
  225.         ::HLock( hText );
  226.         Boolean        wasDroppedInTrash = false;
  227.         result = UDragDropSuit::DragText( inMacEvent, hiliteRgn, textP, textLen, wasDroppedInTrash );
  228.         if ( result && wasDroppedInTrash ) {
  229.             if ( ::ZeroScrap() == noErr ) {
  230.                 ::TECut( teH );
  231.                 if ( ::TEToScrap() != noErr ) ::ZeroScrap();
  232.             //    RefreshEditField();
  233.             }
  234.         }
  235.         ::HUnlock( hText );
  236.     }
  237.  
  238.     if ( hiliteRgn != NULL ) ::DisposeRgn( hiliteRgn );
  239.  
  240.     // Change back the cursor shape to iBeam.
  241.     LEditField::AdjustCursorSelf( inMousePosition, inMacEvent );
  242.  
  243.     return result;
  244. }
  245.  
  246.  
  247. // ==================================================
  248. //    Common functions
  249. // ==================================================
  250.  
  251. // --------------------------------------------------
  252. //        • GetTEActive
  253. // --------------------------------------------------
  254.  
  255. Boolean
  256. CDateEditField::GetTEActive( void )
  257. {
  258.     TEHandle    theTextEditH = GetMacTEH();
  259.  
  260.     return  (Boolean) ( (**theTextEditH).active != 0 );
  261. }
  262.  
  263.  
  264. // --------------------------------------------------
  265. //        • SetTEActive
  266. // --------------------------------------------------
  267.  
  268. void
  269. CDateEditField::SetTEActive(
  270.     const Boolean    inStatus )
  271. {
  272.     TEHandle    theTextEditH = GetMacTEH();
  273.  
  274.     if ( inStatus )  ::TEActivate( theTextEditH );
  275.     else  ::TEDeactivate( theTextEditH );
  276.  
  277.     ::TEIdle( theTextEditH );
  278. }
  279.  
  280.  
  281. // --------------------------------------------------
  282. //        • GetSelect
  283. // --------------------------------------------------
  284.  
  285. void
  286. CDateEditField::GetSelect(
  287.      long &        outSelStart,
  288.      long &        outSelEnd )
  289. {
  290.     TEHandle    theTextEditH = GetMacTEH();
  291.  
  292.     outSelStart = (**theTextEditH).selStart;
  293.     outSelEnd = (**theTextEditH).selEnd;
  294. }
  295.  
  296.  
  297. // --------------------------------------------------
  298. //        • SetSelect
  299. // --------------------------------------------------
  300.  
  301. void
  302. CDateEditField::SetSelect(
  303.     const long        inSelStart,
  304.     const long        inSelEnd )
  305. {
  306.     TEHandle    theTextEditH = GetMacTEH();
  307.     ::TESetSelect( inSelStart, inSelEnd, theTextEditH );
  308. }
  309.  
  310.  
  311. // --------------------------------------------------
  312. //        • GetText
  313. // --------------------------------------------------
  314.  
  315. void
  316. CDateEditField::GetText(
  317.     Str255        outString )
  318. {
  319.     TEHandle    theHandle = GetMacTEH();
  320.  
  321.     outString[0] = (**theHandle).teLength;
  322.     ::BlockMoveData( *((**theHandle).hText), &outString[1], outString[0] );
  323. }
  324.  
  325. // --------------------------------------------------
  326. //        • SetText
  327. // --------------------------------------------------
  328.  
  329. void
  330. CDateEditField::SetText(
  331.     const Str255    inString )
  332. {
  333.     TEHandle    theHandle = GetMacTEH();
  334.  
  335.     ::TESetText( &inString[1], inString[0], theHandle );
  336.  
  337.     Refresh();
  338.  
  339.     mHasChanged = true;
  340. }
  341.  
  342.  
  343. // --------------------------------------------------
  344. //        • SetTextAndRefresh
  345. // --------------------------------------------------
  346.  
  347. void
  348. CDateEditField::SetTextAndRefresh(
  349.     const Str255    inString )
  350. {
  351.     SetText( inString );
  352.     RefreshEditField();
  353. }
  354.  
  355.  
  356. // --------------------------------------------------
  357. //        • SetDateTime
  358. // --------------------------------------------------
  359.  
  360. void
  361. CDateEditField::SetDateTime(
  362.     const unsigned long    inDateTimeSeconds )
  363. {
  364.     PaneIDT    thisPaneID = GetPaneID();
  365.     Str255    theDateString, theTimeString;
  366.  
  367.     UDateTime::SecondsToDateTimeString( inDateTimeSeconds, theDateString, theTimeString );
  368.  
  369.     switch ( thisPaneID ) {
  370.         case kMain_CrTextEditDate:
  371.         case kMain_MdTextEditDate:
  372.             SetText( theDateString );
  373.             break;
  374.  
  375.         case kMain_CrTextEditTime:
  376.         case kMain_MdTextEditTime:
  377.             SetText( theTimeString );
  378.             break;
  379.     }
  380.  
  381.     // Do not need refresh this EditField.
  382.     mHasChanged = false;
  383. }
  384.  
  385.  
  386. // --------------------------------------------------
  387. //        • RefreshEditField
  388. // --------------------------------------------------
  389.  
  390. void
  391. CDateEditField::RefreshEditField( void )
  392. {
  393.     PaneIDT    thisPaneID = GetPaneID();
  394.  
  395.     // Do not refresh the EditField if text has not been changed.
  396.     if ( ! mHasChanged ) return;
  397.  
  398.     // Refresh the EditField, and then reset the flag 'mHasChanged'.
  399.     Str63    theStrDate="\p1", theStrTime="\p0";
  400.     unsigned long    theSeconds;
  401.  
  402.     switch ( thisPaneID ) {
  403.         case kMain_CrTextEditDate:
  404.         case kMain_MdTextEditDate:
  405.         {
  406.             GetText( theStrDate );
  407.             UDateTime::DateTimeStringToSeconds(theStrDate,theStrTime,theSeconds);
  408.             UDateTime::SecondsToDateTimeString(theSeconds,theStrDate,theStrTime);
  409.             SetText( theStrDate );
  410.         }
  411.         break;
  412.  
  413.         case kMain_CrTextEditTime:
  414.         case kMain_MdTextEditTime:
  415.         {
  416.             GetText( theStrTime );
  417.             UDateTime::DateTimeStringToSeconds(theStrDate,theStrTime,theSeconds);
  418.             UDateTime::SecondsToDateTimeString(theSeconds,theStrDate,theStrTime);
  419.             SetText( theStrTime );
  420.         }
  421.         break;
  422.     }
  423.  
  424.     mHasChanged = false;
  425. }
  426.  
  427. // end of program
  428.